#coding=utf-8 
import sys  
reload(sys)  
sys.setdefaultencoding('utf-8') 
import unittest
import logging
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from ec.util import commonUtil
from ec.util import globle


class LangdingTest(unittest.TestCase):

	browser = None

	def __init__(self, methodName,browser):
		self.browser = browser
		unittest.TestCase.__init__(self, methodName)

	def testLoginInSuccess(self):
		logging.info("start check checkLoginInSuccess")
		self.browser.get(globle.WEB_URL)
		if("/portal/products" in self.browser.current_url):
			self.testLoginOut()

		username = self.browser.find_element_by_name("username")
		username.clear()
		username.send_keys("zxzbszd")
		password = self.browser.find_element_by_name("password")
		password.clear()
		password.send_keys("123456")
		submitBtn = self.browser.find_element_by_class_name("btn-submit")
		submitBtn.send_keys(Keys.RETURN)
		try:
			WebDriverWait(self.browser, 5).until(lambda x: "/portal/products" in self.browser.current_url)
		except:
			commonUtil.saveErrorImage(self.browser,r"report\errorImage\login\checkLoginInSuccess.png")
			self.assertTrue(False,"checkLoginIn 正确用户名密码登陆测试未通过<errorImage\login\checkLoginInSuccess.png>")

	def testLoginInFail(self):
		logging.info("start check checkLoginInFail")
		self.browser.get(globle.WEB_URL)
		if("/portal/products" in self.browser.current_url):
			self.testLoginOut()

		username = self.browser.find_element_by_name("username")
		username.clear()
		username.send_keys("zxzbszd1")
		password = self.browser.find_element_by_name("password")
		password.clear()
		password.send_keys("123456")
		submitBtn = self.browser.find_element_by_class_name("btn-submit")
		submitBtn.send_keys(Keys.RETURN)
		try:
			WebDriverWait(self.browser, 5).until(lambda x: self.browser.find_element_by_class_name("errors1"))
		except:
			commonUtil.saveErrorImage(self.browser,r"report\errorImage\login\checkLoginInFail.png")
			self.assertTrue(False,"checkLoginInFail 错误用户名密码登陆测试未通过<errorImage\login\checkLoginInFail.png>")

	def testLoginOut(self):
		logging.info("start check loginOut")
		exitHref = None
		try:
			exitHref = WebDriverWait(self.browser, 5).until(lambda x:self.browser.find_element_by_class_name("top-welcome-exit"))
		except:
			commonUtil.saveErrorImage(self.browser,r"report\errorImage\login\checkLoginOut.png")
			self.assertTrue(False,"checkLoginOut 退出登录测试失败，状态为未登录<errorImage\login\checkLoginOut.png>")

		exitHref.click()
		try:
			WebDriverWait(self.browser, 5).until(lambda x: "/accounts/login" in self.browser.current_url)
		except:
			commonUtil.saveErrorImage(self.browser,r"report\errorImage\login\checkLoginOut.png")
			self.assertTrue(False,"checkLoginOut 退出登录测试失败,未成功退出登陆<errorImage\login\checkLoginOut.png>")

	def testLogin(self):
		self.testLoginInFail()

		self.testLoginInSuccess()